/*************************************************************
 * Title: PassportResource.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com
 * CreateTime: Feb 22, 2013 4:55:28 PM
 ************************************************************/
package snow.biz.system.web.rest;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.restlet.data.Form;

import snow.biz.system.model.User;
import snow.biz.system.service.IUserService;
import snow.core.context.AccessContext;
import snow.core.enums.Status;
import snow.core.exceptions.BusinessException;
import snow.core.persistence.AndedConditions;
import snow.core.persistence.BinaryOperator;
import snow.core.persistence.Condition;
import snow.core.persistence.Conditions;
import snow.core.persistence.EntityHelper;
import snow.core.security.Operator;
import snow.core.security.Principal;
import snow.core.security.annotations.Operation;
import snow.core.security.annotations.Resource;
import snow.core.toolkit.Validator;
import snow.core.toolkit.security.Cryptogram;
import snow.core.web.rest.ResourceSupport;

/**
 * 系统登录、登出
 */
@Path("/system/passport")
@Resource(uri = "/passport")
public class PassportResource extends ResourceSupport {

	private IUserService userService;

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Operation(name = "用户登录", type = Operator.SEARCH, needCheck = false)
	public Object obtainPassport() throws BusinessException {
		final Form queryForm = super.getQueryAsForm();
		final String loginName = queryForm.getFirstValue("loginName");
		final String loginPass = queryForm.getFirstValue("loginPass");
		final String validCode = queryForm.getFirstValue("validCode");
		if (log.isInfoEnabled()) {
			log.info("Login Name is -> " + loginName);
			log.info("Login Pass is -> " + loginPass);
			log.info("Valid Code is -> " + validCode);
		}
		final AccessContext context = AccessContext.getCurrent();
		final String sessionValidCode = context.getFromSession("login_valid_code", String.class);
		super.removeSessionValueFrom("login_valid_code");
		Validator.notBlank(validCode, "200101", "failure.system.passport.retrieved.valid.code");
		boolean equals = StringUtils.equalsIgnoreCase(validCode, sessionValidCode);
		Validator.isTrue(equals, "200102", "failure.system.passport.incorrect.valid.code");
		Validator.notBlank(loginName, "200103", "failure.system.user.login.name.blank");
		final Conditions conditions = new AndedConditions();
		conditions.addCondition(new Condition("loginName", BinaryOperator.EQ, loginName.trim()));
		conditions.addCondition(new Condition("status", BinaryOperator.EQ, Status.SUCCESS));
		final List<User> userList = userService.searchUsersFrom(conditions, null, -1, -1);
		Validator.notEmpty(userList, "200104", "failure.system.passport.login.name.not.exist");
		final User user = userList.get(0);
		Validator.isTrue(EntityHelper.validate(user), "200105", "failure.system.passport.user.status.failure");
		Validator.notBlank(loginPass, "200106", "failure.system.user.login.pass.blank");
		equals = StringUtils.equalsIgnoreCase(user.getLoginPass(), Cryptogram.encodeBySHA(loginPass));
		Validator.isTrue(equals, "200107", "failure.system.passport.incorrect.login.pass");
		super.putSessionValueFrom(Principal.CURRENT_USER_ID, user.getId());
		return getSuccessResultFrom("success.passport.login");
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Operation(name = "退出登录", type = Operator.REMOVE, needCheck = false)
	public Object cancelPassport() {
		return null;
	}

	public IUserService getUserService() {
		return this.userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}
